<?php
require ('../include/init.inc.php');
$user_id = $user_name = $user_phone = $user_password = $user_group = $user_sex = $real_name = $user_id_type
= $user_num = $user_duty = $user_register_time = $user_photo = '';
extract ( $_POST, EXTR_IF_EXISTS );

if (Common::isPost ()) {
    
    $pic = $_FILES['user_photo']['tmp_name'];
    if($pic == "") {
        Common::exitWithError(ErrorMessage::PHOTO_ERROR,"panel/family_group_user.php");
    }
    $upload_ret = false;
    if($pic){
        // 上传的路径
        $photoDir = $_SERVER['DOCUMENT_ROOT']."/photos/FamilyGroup";
        // 创建文件夹
        if(!file_exists($photoDir)){
            mkdir($photoDir, 0777);
        }
        // 用时间戳来保存图片，防止重复
        $timeNow = time();
        $targetFile = $photoDir . '/' . $timeNow.'.jpg';
        $sqlFile = "/photos/FamilyGroup/".$timeNow.'.jpg';
        echo $targetFile."<br / >";
        // 将临时文件 移动到我们指定的路径，返回上传结果
        $upload_ret = move_uploaded_file($pic, $targetFile) ? true : false;
    }
    if($upload_ret == false){
        Common::exitWithError(ErrorMessage::PHOTO_ERROR,"panel/family_group_user.php");
    }
	$exist = FamilyGroupUser::getUserById($user_id);
	if($exist){
		OSAdmin::alert("error",ErrorMessage::NAME_CONFLICT);
	}
	else if($user_password =="" || $user_group <= 0 ){
		
		OSAdmin::alert("error",ErrorMessage::NEED_PARAM);
	}
	else{
		$input_data = array ('user_id' => $user_id, 'user_name' => $user_name, 'user_password' => md5 ( $user_password ), 
		    'user_group' => $user_group,
		    'user_sex' => $user_sex, 'real_name' => $real_name, 'user_id_type' => $user_id_type, 'user_num' => $user_num
		    , 'user_duty' => $user_duty, 'user_register_time' => $user_register_time, 'user_photo' => $sqlFile
		    , 'user_phone' => $user_phone
		);
		$user_id = FamilyGroupUser::addUser ( $input_data );
		
		if ($user_id == 0) {
			$input_data['password']="";
			SysLog::addLog ( UserSession::getUserName(), 'ADD', 'User' ,$user_id, json_encode($input_data) );
			Common::exitWithSuccess ('账号添加成功','panel/family_group_user.php');
		}else{
			OSAdmin::alert("error");
		}
	}
}
$group_options = UserGroup::getGroupForOptions();
// 获取旅游客户id
$user_id_post = FamilyGroupUser::getMaxId();
Template::assign("user_id_post" ,$user_id_post);
Template::assign("_POST" ,$_POST);
Template::assign ( 'group_options', $group_options );
Template::display ( 'panel/family_group_user_add.tpl' );
